local hx__juzichiju = fk.CreateSkill {
  name = "hx__juzichiju",
}


hx__juzichiju:addEffect("active", {
  anim_type = "control",
  prompt = function(self, player)
    return "你可以失去一个“橘”然后选择一项效果执行"
  end,
  expand_pile = "hx__wyorz_juzi",
  card_num = 1,
  target_num = 0,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and player:getPileNameOfId(to_select) == "hx__wyorz_juzi"
  end,
  target_filter = Util.FalseFunc,
  interaction = function(self, player)
    local choices = {}
    local record = player:getTableMark("hx__juzichiju-phase")
    local num = math.max(player:getMark("@hx__juzichiju_count"), 0)
    if not table.contains(record, 1) then
      table.insert(choices, "hx__juzichiju_draw:::"..num)
    end
    if not table.contains(record, 2) then
      table.insert(choices, "recover")
    end
    if not table.contains(record, 3) then
      table.insert(choices, "hx__juzichiju_maxhp")
    end
    if not table.contains(record, 4) then
      table.insert(choices, "hx__juzichiju_shield")
    end
    local all_choices = {
      "hx__juzichiju_draw:::"..num,
      "recover",
      "hx__juzichiju_maxhp",
      "hx__juzichiju_shield",
    }
    return UI.ComboBox { choices = choices, all_choices = all_choices}
  end,
  can_use = function(self, player)
    return #player:getTableMark("hx__juzichiju-phase") < 4 and #player:getPile("hx__wyorz_juzi") > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:moveCardTo(effect.cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, hx__juzichiju.name, nil, true, player)
    if self.interaction.data:startsWith("hx__juzichiju_draw") then
      room:addTableMark(player, "hx__juzichiju-phase", 1)
      room:addPlayerMark(player, "@hx__juzichiju_count", 1)
      local num = math.max(player:getMark("@hx__juzichiju_count")-1, 0)
      player:drawCards(num, hx__juzichiju.name)
    elseif self.interaction.data:startsWith("recover") then
      room:addTableMark(player, "hx__juzichiju-phase", 2)
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = hx__juzichiju.name,
      }
    elseif self.interaction.data:startsWith("hx__juzichiju_maxhp") then
      room:addTableMark(player, "hx__juzichiju-phase", 3)
      room:changeMaxHp(player, 1)
    elseif self.interaction.data:startsWith("hx__juzichiju_shield") then
      room:addTableMark(player, "hx__juzichiju-phase", 4)
      room:changeShield(player, 1)
    end
    if #player:getPile("hx__wyorz_juzi") >= 5 then
      if not player:hasSkill("hx__juzichiju",true) then
        room:handleAddLoseSkills(player, "hx__juzichiju")
      else
        player:setSkillUseHistory("hx__juzichiju", 0, Player.HistoryGame)
        room:setPlayerMark(player, "hx__juzichiju-phase", 0)
        room:sendLog{
          type = "%from 的“吃橘”已被重置",
          from = player.id,
        }
      end
    end
  end,
})


return hx__juzichiju
